import optparse
import os.path

def _opt_abspath(opt, optstr, value, op):
    setattr(op.values, opt.dest, os.path.abspath(value))

_options = {'job-name': optparse.make_option("--bcat-job-name", dest='bcat_job_name', default=None,
                                             help='Name jobs NAME in SLURM queue', 
                                             metavar='NAME'),
            'outdir': optparse.make_option("--bcat-outdir", dest='bcat_outdir', default=None,
                                           callback=_opt_abspath, action='callback', type='str',
                                           help='Leave output files in DIR', metavar='DIR'),
            'duration': optparse.make_option("--bcat-duration", dest='bcat_duration', 
                                             default='08:00:00', help='Set job duration of N minutes or HH:MM:SS',
                                             metavar='N (or) HH:MM:SS'),
            'nnodes': optparse.make_option("--bcat-nodes", dest='bcat_nnodes', type='int', default=1,
                                           help='Use N nodes per job', metavar='N'),
            'stdout': optparse.make_option("--bcat-stdout", dest='bcat_stdout', default=None,
                                           callback=_opt_abspath, action='callback', type='str',
                                           help='Redirect job stdout to PATH', metavar='PATH'),
            'stderr': optparse.make_option("--bcat-stderr", dest='bcat_stderr', default=None,
                                           callback=_opt_abspath, action='callback', type='str',
                                           help='Redirect job stderr to PATH', metavar='PATH'),
            'stdoutdir': optparse.make_option("--bcat-stdout-dir", dest='bcat_stdoutdir', default=None,
                                              callback=_opt_abspath, action='callback', type='str',
                                              help='Place job stdout in DIR', metavar='DIR'),
            'stderrdir': optparse.make_option("--bcat-stderr-dir", dest='bcat_stderrdir', default=None,
                                              callback=_opt_abspath, action='callback', type='str',
                                              help='Place job stderr in DIR', metavar='DIR'),
            'write': optparse.make_option("--bcat-write", dest='bcat_write', default=None,
                                          help='Write scripts to DIR instead of submitting', metavar='DIR'),
            }


def add_options(op, omit=[]):
    optgroup = optparse.OptionGroup(op, "batchcat common options")

    for k in _options:
        if k in omit:
            continue
        optgroup.add_option(_options[k])

    op.add_option_group(optgroup)

